/**
 * 任务相关接口
 */
import { request } from "../plugins/request";

// add task
export const addTask = ({ project_id, folder_id, name, operator, content, description }) => {
  let url = "/api/idata/tasks";

  let task = {
    project_id,
    folder_id,
    name,
    operator,
    content,
    description
  };

  return request.post(url, task);
};
export const downloadTaskLog = hostname => {
  let url = "/api/idata/tasks/log/download";
  return request
    .get(url, {
      params: {
        hostname
      },
      headers: {
        responseType: "blob"
      }
    })
    .then(res => {
      const blob = new Blob([res], { type: "application/octet-stream" });
      let dom = document.createElement("a");
      let url = window.URL.createObjectURL(blob);
      dom.href = url;
      dom.download = decodeURI(hostname + ".log");
      dom.style.display = "none";
      document.body.appendChild(dom);
      dom.click();
      dom.parentNode.removeChild(dom);
      window.URL.revokeObjectURL(url);
    });
};

export const task = {
  addTask: {
    post: "/api/idata/tasks"
  },
  updateTask: {
    patch: "/api/idata/tasks/{id}"
  },
  getTaskList: {
    get: "/api/idata/tasks?project_id={project_id}&folder_id={folder_id}"
  },
  getTaskById: {
    get: "/api/idata/tasks/{id}"
  },
  deleteTask: {
    delete: "/api/idata/tasks/{id}"
  },

  runTask: {
    post: "/api/idata/tasks/{task_id}/trigger"
  },
  stopTask: {
    post: "/api/idata/tasks/kill"
  },
  getTaskHistory: {
    get: "/api/idata/tasks/history_instance?task_id={task_id}"
  },
  getRefWorkflow: {
    get: "/api/idata/tasks/{id}/workflow"
  },
  getTaskTypes: {
    get: "/api/idata/tasks/type"
  },
  downloadTaskLog: {
    get: "/api/idata/tasks/log/download?hostname={hostname}&operator={operator}&start={start}"
  },
  getTaskLog: {
    get: "/api/idata/tasks/log?dag_id={dag_id}&task_id={task_id}"
  },
  getTaskLastInstance: {
    get: "/api/idata/tasks/{id}/last_instance"
  },
  addTaskWarning: {
    post: "/api/idata/tasks/alert"
  },
  deleteTaskWarning: {
    delete: "/api/idata/tasks/alert"
  },
  getAlertTypes: {
    get: "/api/idata/alerts/options?project_id={project_id}&category={category}"
  },
  getAlerts: {
    get: "/api/idata/tasks/{task_id}/alerts"
  },
  saveAlert: {
    post: "/api/idata/tasks/{task_id}/alerts"
  },
  cloneTask: {
    post: "/api/idata/tasks/{task_id}/clone"
  },
  getDatafactoryTypes: {
    get: "/api/idata/tasks/datafactory/plugin?type={type}"
  },
  datasources: {
    get: "/api/idata/datasources?project_id={project_id}&type={type}"
  },
  fieldtypes: {
    get: "/api/idata/tasks/datafactory/fieldtypes"
  },
  envs: {
    get: "/api/idata/envs"
  },
  run_resource: {
    get: "/api/idata/tasks/datafactory/run_resource"
  }
};
